package com.ofek.eventos.notyet;

import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.ofek.eventos.Constants;
import com.ofek.eventos.EventGroup;
import com.ofek.eventos.R;
import com.ofek.eventos.db.EventosDatabaseService;
import com.ofek.eventos.db.datatypes.EventData;
import com.ofek.eventos.db.datatypes.helptypes.TextFieldInEvent;

public class CategorizedListAdapter extends BaseExpandableListAdapter {

	private final List<EventGroup> groupModel;
	private final Activity context;
	private boolean edit;

	public void setEditState(final boolean edit) {
		this.edit = edit;
		notifyDataSetInvalidated();
	}

	public CategorizedListAdapter(final Activity c, final EventGroup... groupModel) {
		this.groupModel =Arrays.asList(groupModel);
		context = c;
		LocalBroadcastManager.getInstance(c).registerReceiver(
				new BroadcastReceiver() {

					@Override
					public void onReceive(final Context context,
							final Intent intent) {
						notifyDataSetChanged();
					}

				}, new IntentFilter(Constants.EVENT_NOTIFY_EVENT_SET_CHANGED));
	}
	public CategorizedListAdapter(final Activity c, final List<EventGroup> groupModel) {
		this.groupModel = groupModel;
		context = c;
		LocalBroadcastManager.getInstance(c).registerReceiver(
				new BroadcastReceiver() {

					@Override
					public void onReceive(final Context context,
							final Intent intent) {
						notifyDataSetChanged();
					}

				}, new IntentFilter(Constants.EVENT_NOTIFY_EVENT_SET_CHANGED));
	}
	@Override
	public View getChildView(final int groupPosition, final int childPosition,
			final boolean isLastChild, View convertView, final ViewGroup parent) {
		View delete = null;
		final EventData event = groupModel.get(groupPosition).get(childPosition);
		if (convertView == null) {
			final LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.list_item_event, null);
		}
		final TextView title = (TextView) convertView
				.findViewById(R.id.title);
		title.setText(((TextFieldInEvent)event.values.get((long)0)).value);
		//		final View unconfirmed = convertView.findViewById(R.id.unconfirmed);
		//unconfirmed.setVisibility(!edit && event.confirmed ? View.INVISIBLE : View.VISIBLE);
		delete = convertView.findViewById(R.id.delete);
		delete.setVisibility(edit ? View.VISIBLE : View.INVISIBLE);
		delete.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(final View v) {
				deleteEvent(groupPosition, event);
			}
		});
		return convertView;
	}

	private void deleteEvent(final int groupPosition, final EventData event) {
		final Intent intent = new Intent(EventosDatabaseService.EVENT_REMOVE_REQUEST);
		intent.putExtra(EventosDatabaseService.EVENT_EXTRA, event);
		LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
		groupModel.get(groupPosition).remove(event);
		notifyDataSetChanged();
	}

	@Override
	public View getGroupView(final int groupPosition, final boolean isExpanded,
			View convertView, final ViewGroup parent) {
		if (convertView != null) {
			final TextView title = (TextView) convertView
					.findViewById(R.id.title);
			title.setText(groupModel.get(groupPosition).getTitleResource());
		} else {
			final LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.list_group_header, null);
			final TextView title = (TextView) convertView
					.findViewById(R.id.title);
			title.setText(groupModel.get(groupPosition).getTitleResource());
		}

		return convertView;
	}

	@Override
	public boolean isChildSelectable(final int groupPosition,
			final int childPosition) {
		return true;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public Object getChild(final int groupPosition, final int childPosition) {
		return groupModel.get(groupPosition).get(childPosition);
	}

	@Override
	public long getChildId(final int groupPosition, final int childPosition) {
		return groupModel.get(groupPosition).get(childPosition).eid;
	}

	@Override
	public int getChildrenCount(final int groupPosition) {
		return groupModel.get(groupPosition).getCount();
	}

	@Override
	public Object getGroup(final int groupPosition) {
		return groupModel.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return groupModel.size();
	}

	@Override
	public long getGroupId(final int groupPosition) {
		return groupPosition;
	}

	public void flipEditState() {
		edit = !edit;
		notifyDataSetInvalidated();
	}

}
